package com.qumitech.visualnovel.service.button.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.button.ButtonConfigVO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.button.ButtonService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/9 22:27
 */

@Service
public class ButtonServiceImpl implements ButtonService {

    private final String BUTTON_URL = "/button";
    private final String BUTTON_ADMIN_URL = "/admin/button";


    @Override
    public Map<String, Object> listButtonConfigVO(Integer offset, Integer limit, String buttonId, String buttonName) {
        String url = UrlConstant.SERVER_URL + BUTTON_ADMIN_URL;
        Map<String, String> paramMap = new HashMap<>();

        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (buttonId != null) {
            paramMap.put("button_id", buttonId);
        }
        if (buttonName != null) {
            paramMap.put("button_name", buttonName);
        }


        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addButtonConfig(ButtonConfigVO buttonConfig) {
        String url = UrlConstant.SERVER_URL + BUTTON_URL;
        String req = JSONObject.toJSONString(buttonConfig);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeButtonConfig(String buttonId) {
        String url = UrlConstant.SERVER_URL + BUTTON_URL + "/" + buttonId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {

            Integer status = Integer.valueOf(((Map<Integer, Object>) rsp.getError()).get("status").toString());
            if (status.equals(ResultEnum.BLOCK_BUTTON_BE_USED.getState())) {
                throw new BusinessException(ResultEnum.BLOCK_BUTTON_BE_USED);
            }
            throw new BusinessException(ResultEnum.FAILURE);

        }
    }

    @Override
    public void updateButtonConfig(ButtonConfigVO buttonConfig) {
        String url = UrlConstant.SERVER_URL + BUTTON_URL + "/" + buttonConfig.getButtonId();
        String req = JSONObject.toJSONString(buttonConfig);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }
}
